package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.TicketFlow;
import com.bestcem.xm.ticket.entity.mysql.TicketFlowDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 10:43
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketFlowConverter {

    @Nullable
    public static TicketFlow doToDto(TicketFlowDO ticketFlowDO) {
        if (Objects.isNull(ticketFlowDO)) {
            return null;
        }

        TicketFlow ticketFlow = new TicketFlow();
        ticketFlow.setId(ticketFlowDO.getTicketFlowId());
        ticketFlow.setOrgId(ticketFlowDO.getOrgId());
        ticketFlow.setUserId(ticketFlowDO.getUserId());
        ticketFlow.setTicketConfigId(ticketFlowDO.getTicketConfigId());
        ticketFlow.setFlowId(ticketFlowDO.getFlowId());
        ticketFlow.setTitle(ticketFlowDO.getTitle());
        ticketFlow.setStatus(ticketFlowDO.getStatus());
        ticketFlow.setMatchCondition(ticketFlowDO.getMatchCondition());
        ticketFlow.setAlertRuleIds(CommonConverter.stringToList(ticketFlowDO.getAlertRuleIds(), String.class));
        ticketFlow.setCustomRuleIds(CommonConverter
                .stringToList(ticketFlowDO.getCustomRuleIds(), TicketFlow.CustomRuleId.class));
        ticketFlow.setPriority(ticketFlowDO.getPriority());
        ticketFlow.setFollowConfig(CommonConverter
                .stringToList(ticketFlowDO.getFollowConfig(), TicketFlow.FollowConfig.class));
        ticketFlow.setNeedAudit(ticketFlowDO.getNeedAudit());
        ticketFlow.setAuditConfig(CommonConverter
                .stringToList(ticketFlowDO.getAuditConfig(), TicketFlow.FollowConfig.class));
        ticketFlow.setVersion(ticketFlowDO.getVersion());
        ticketFlow.setRuleList(CommonConverter.stringToList(ticketFlowDO.getRuleList(), String.class));
        ticketFlow.setSample(ticketFlowDO.getSample());

        CommonConverter.setEntity(ticketFlow, ticketFlowDO);
        return ticketFlow;
    }

    @Nonnull
    public static List<TicketFlow> doListToDtoList(List<TicketFlowDO> ticketFlowDOList) {
        List<TicketFlow> list = new ArrayList<>();
        if (Objects.isNull(ticketFlowDOList)) {
            return list;
        }

        for (TicketFlowDO ticketFlowDO : ticketFlowDOList) {
            TicketFlow ticketFlow = doToDto(ticketFlowDO);
            list.add(ticketFlow);
        }
        return list;
    }

    @Nullable
    public static TicketFlowDO dtoToDo(TicketFlow ticketFlow) {
        if (Objects.isNull(ticketFlow)) {
            return null;
        }

        TicketFlowDO ticketFlowDO = new TicketFlowDO();
        ticketFlowDO.setTicketFlowId(ticketFlow.getId());
        ticketFlowDO.setOrgId(ticketFlow.getOrgId());
        ticketFlowDO.setUserId(ticketFlow.getUserId());
        ticketFlowDO.setTicketConfigId(ticketFlow.getTicketConfigId());
        ticketFlowDO.setFlowId(ticketFlow.getFlowId());
        ticketFlowDO.setTitle(ticketFlow.getTitle());
        ticketFlowDO.setStatus(ticketFlow.getStatus());
        ticketFlowDO.setMatchCondition(ticketFlow.getMatchCondition());
        ticketFlowDO.setAlertRuleIds(CommonConverter.listToString(ticketFlow.getAlertRuleIds()));
        ticketFlowDO.setCustomRuleIds(CommonConverter.listToString(ticketFlow.getCustomRuleIds()));
        ticketFlowDO.setPriority(ticketFlow.getPriority());
        ticketFlowDO.setFollowConfig(CommonConverter.listToString(ticketFlow.getFollowConfig()));
        ticketFlowDO.setNeedAudit(ticketFlow.getNeedAudit());
        ticketFlowDO.setAuditConfig(CommonConverter.listToString(ticketFlow.getAuditConfig()));
        ticketFlowDO.setVersion(ticketFlow.getVersion());
        ticketFlowDO.setRuleList(CommonConverter.listToString(ticketFlow.getRuleList()));
        ticketFlowDO.setSample(ticketFlow.getSample());

        CommonConverter.setDo(ticketFlow, ticketFlowDO);
        return ticketFlowDO;
    }
}
